/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.mvc.kungkher.rego;

import com.kungkhies.domain.kungkher.Kungkher;
import com.kungkhies.domain.kungkher.KungkherRegoFormObject;
import com.kungkhies.domain.kungkher.KungkherRegoFormObjectValidator;
import com.kungkhies.persistence.kungkher.JpaKungkherDaoImpl;
import com.kungkhies.services.kungkher.KungkherService;
import com.kungkhies.services.kungkher.KungkherServiceImpl;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author clayton
 */
@Controller
@SessionAttributes("kungkherRegoFormObject")
public class KungkherRegoController
{
    
    private KungkherService kungkherService;

    public KungkherRegoController()
    {
    }
    
    @InitBinder
    protected void initBinder(WebDataBinder binder)
    {
        KungkherRegoFormObjectValidator val = new KungkherRegoFormObjectValidator();
        KungkherServiceImpl kungkherServiceImpl = (KungkherServiceImpl) kungkherService;
        JpaKungkherDaoImpl jpa = new JpaKungkherDaoImpl();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("kungkhiePU");
        jpa.setEntityManagerFactory(emf);
        kungkherServiceImpl.setKungkherDao(jpa);
        val.setKungkherService(kungkherServiceImpl);
        
        binder.setValidator(val);
    }

    @ModelAttribute("kungkherRegoFormObject")
    public KungkherRegoFormObject createKungkherRegoFormObject()
    {
        return new KungkherRegoFormObject();
    }

    @RequestMapping(value =
    {
        "/kungkhersRego"
    }, method = RequestMethod.GET)
    public void form()
    {
    }

    @RequestMapping(value =
    {
        "/kungkherRego"
    }, method = RequestMethod.POST)
    public String registerKungkherFromForm(@Valid KungkherRegoFormObject kungkherRegoFormObject,
            BindingResult bindingResult,
            @RequestParam(value = "imageFile", required = false) MultipartFile image)
    {

        //kungkher = createKungkher();

        if (bindingResult.hasErrors())
        {
            return "kungkhers/rego";
        }

        if (kungkherRegoFormObject != null)
        {
            System.out.println("RegoFormObject NOT null in controller");
        }
        System.out.println("kungkherRegoFullName: " + kungkherRegoFormObject.getFullName());
        System.out.println("kungkherRegoKungkherName: " + kungkherRegoFormObject.getKungkherName());
        System.out.println("kungkherRegoPassword: " + kungkherRegoFormObject.getPassword());
        System.out.println("kungkherRegoEmail: " + kungkherRegoFormObject.getEmail());
        System.out.println("kungkherRegoSendMeEmails: " + kungkherRegoFormObject.isUpdateByEmail());
        
        Kungkher kungkher = new Kungkher();
        
        kungkher.setFullName(kungkherRegoFormObject.getFullName());
        kungkher.setKungkherName(kungkherRegoFormObject.getKungkherName());
        kungkher.setEmail(kungkherRegoFormObject.getEmail());
        kungkher.setPassword(kungkherRegoFormObject.getPassword());
        kungkher.setUpdateByEmail(kungkherRegoFormObject.isUpdateByEmail());
        kungkher.setInterests(kungkherRegoFormObject.getInterests());
        
        if (kungkherService == null)
        {
            System.out.println("kungkherService is NULL in RegoController");
        }
        else
        {
            System.out.println("kungkherService is NOT NULL in RegoController");
        }
        kungkherService.saveKungkher(kungkher);

        return "home";
    }
    
    @Autowired
    public void setKungkherService(KungkherService kungkherService)
    {
        this.kungkherService = kungkherService;
    }
}
